package com.tianzeculture.stagebao.activity.home.popwindow;

import android.app.Activity;
import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;

import com.tianzeculture.stagebao.R;

/**
 * Created by Administrator on 2018/6/5 0005.
 * 招募类型
 */

public class NoticeTypePopWindow extends PopupWindow implements View.OnClickListener {
    private Activity mContext;
    private final LayoutInflater inflater;
    private View styleView;
    private final Display display;
    private final WindowManager windowManager;
    private NoticeTypeListener noticeTypeListener;

    CheckBox boxLuyan, boxPaishe, boxHuodong, boxXuanchuan;
    ImageView ivLuyan, ivPaishe, ivHuodong, ivXuanchuan;

    public NoticeTypePopWindow(Activity context, String checkType) {
        this.mContext = context;
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        styleView = inflater.inflate(R.layout.popwindow_notice_type, null);

        this.setContentView(styleView);
        //设置CityPickerPopWindow弹出窗体的宽
        windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        display = windowManager.getDefaultDisplay();
        this.setWidth(RelativeLayout.LayoutParams.FILL_PARENT);
        //设置CityPickerPopWindow弹出窗体的高
//        this.setHeight((int) (display.getHeight() * 0.35));
        this.setHeight((int) (RelativeLayout.LayoutParams.WRAP_CONTENT));

        //设置CityPickerPopWindow弹出窗体可点击
        this.setFocusable(true);
        //设置CityPickerPopWindow可触摸
        this.setTouchable(true);
        //设置CityPickerPopWindow弹出窗体动画效果
//        this.setAnimationStyle(R.style.AnimBottom);
        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0x9000000);
        //设置CityPickerPopWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        this.setOutsideTouchable(true);
        setUpListener(styleView, checkType);
    }


    public void setUpListener(View styleView, String checkType) {
        boxLuyan = styleView.findViewById(R.id.boxLuyan);
        boxPaishe = styleView.findViewById(R.id.boxPaishe);
        boxHuodong = styleView.findViewById(R.id.boxHuodong);
        boxXuanchuan = styleView.findViewById(R.id.boxXuanchuan);
        boxLuyan.setOnClickListener(this);
        boxPaishe.setOnClickListener(this);
        boxHuodong.setOnClickListener(this);
        boxXuanchuan.setOnClickListener(this);

        ivLuyan = styleView.findViewById(R.id.ivLuyan);
        ivPaishe = styleView.findViewById(R.id.ivPaishe);
        ivHuodong = styleView.findViewById(R.id.ivHuodong);
        ivXuanchuan = styleView.findViewById(R.id.ivXuanchuan);

        if (checkType == null)
            checkType = "";
        if (!"".equals(checkType)) {
            String[] strings = checkType.split(",");
            for (int i = 0; i < strings.length; i++) {
                if (strings[i].equals(boxLuyan.getText().toString().trim())) {
                    ivLuyan.setVisibility(View.VISIBLE);
                    boxLuyan.setChecked(true);
                } else if (strings[i].equals(boxPaishe.getText().toString().trim())) {
                    ivPaishe.setVisibility(View.VISIBLE);
                    boxPaishe.setChecked(true);
                } else if (strings[i].equals(boxHuodong.getText().toString().trim())) {
                    ivHuodong.setVisibility(View.VISIBLE);
                    boxHuodong.setChecked(true);
                } else if (strings[i].equals(boxXuanchuan.getText().toString().trim())) {
                    ivXuanchuan.setVisibility(View.VISIBLE);
                    boxXuanchuan.setChecked(true);
                }
            }
        }


        Button btnSure = styleView.findViewById(R.id.btnSure);
        btnSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String checkType = "";
                if (boxLuyan.isChecked()) {
//                    checkType += boxLuyan.getText().toString().trim() + ",";
                    checkType += 0 + ",";
                }
                if (boxPaishe.isChecked()) {
//                    checkType += boxPaishe.getText().toString().trim() + ",";
                    checkType += 1 + ",";
                }
                if (boxHuodong.isChecked()) {
//                    checkType += boxHuodong.getText().toString().trim() + ",";
                    checkType += 2 + ",";
                }
                if (boxXuanchuan.isChecked()) {
//                    checkType += boxXuanchuan.getText().toString().trim() + ",";
                    checkType += 3 + ",";
                }
                if (checkType.length() > 0) {
                    checkType = checkType.substring(0, checkType.length() - 1);
                }
                noticeTypeListener.pickNoticeTypeValue(checkType);
                dismiss();
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.boxLuyan:
                if (boxLuyan.isChecked())
                    ivLuyan.setVisibility(View.VISIBLE);
                else
                    ivLuyan.setVisibility(View.GONE);
                break;
            case R.id.boxPaishe:
                if (boxPaishe.isChecked())
                    ivPaishe.setVisibility(View.VISIBLE);
                else
                    ivPaishe.setVisibility(View.GONE);
                break;
            case R.id.boxHuodong:
                if (boxHuodong.isChecked())
                    ivHuodong.setVisibility(View.VISIBLE);
                else
                    ivHuodong.setVisibility(View.GONE);
                break;
            case R.id.boxXuanchuan:
                if (boxXuanchuan.isChecked())
                    ivXuanchuan.setVisibility(View.VISIBLE);
                else
                    ivXuanchuan.setVisibility(View.GONE);
                break;
        }
    }

    /**
     * 显示popupWindow
     *
     * @param parent
     */
    public void showPopupWindow(View parent) {
        if (!this.isShowing()) {
            // 以下拉方式显示popupwindow
//            this.showAtLocation(parent, Gravity.CENTER_HORIZONTAL, 0, 0); //设置layout在PopupWindow中显示的位置
            this.showAsDropDown(parent);
        } else {
            this.dismiss();
        }
    }


    public interface NoticeTypeListener {
        void pickNoticeTypeValue(String value);

        void noticeTypeCancel();
    }

    public void setNoticeTypeListener(NoticeTypeListener listener) {
        this.noticeTypeListener = listener;
    }

    @Override
    public void showAsDropDown(View anchor) {
        if (Build.VERSION.SDK_INT >= 24) {
            Rect rect = new Rect();
            anchor.getGlobalVisibleRect(rect);
            int h = anchor.getResources().getDisplayMetrics().heightPixels - rect.bottom;
            setHeight(h);
        }
        super.showAsDropDown(anchor);
    }

    @Override
    public void dismiss() {
        super.dismiss();
        noticeTypeListener.noticeTypeCancel();
    }
}
